package com.starsoft.trade.trader;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.starsoft.smdc.bean.SmdcSecurity;
import com.starsoft.smdc.service.HolidayService;
import com.starsoft.smdc.service.SecurityService;
import com.starsoft.smdc.util.SecurityType;
import com.starsoft.trade.bean.TradeAcount;
import com.starsoft.trade.bean.TradeAcountStatus;
import com.starsoft.trade.bean.TradeBill;
import com.starsoft.trade.bean.TradePosition;
import com.starsoft.trade.service.TradeBillService;

@Component
public class TraderContextService {
	
	@Autowired
	SecurityService securityService;
	
	@Autowired
	HolidayService holidayService;
	
	@Autowired
	TradeBillService billService;
	
	public SecurityService getSecurityService() {
		return securityService;
	}

	public HolidayService getHolidayService() {
		return holidayService;
	}

	public TradeBillService getBillService() {
		return billService;
	}

	public TradeAcountStatus fillStatus(TradeAcount acount, TradeAcountStatus status){
		status.setTradeAcount(acount);
		status.getSummary().getId().setAcountId(acount.getAcountId());
		for(TradePosition pos : status.getPositions()){
			pos.getId().setAcountId(acount.getAcountId());
		}
		return status;
	}
	
	public List<TradeBill> fillBills(TradeAcount acount, List<TradeBill> bills){
		for(TradeBill bill : bills){
			bill.setAcountId(acount.getAcountId());
			billService.dealBillType(bill);
		}
		return bills;
	}
	
	public SmdcSecurity getSecurityByTicker(String ticker) {
		List<SmdcSecurity> securities = securityService.getByTicker(ticker);
		SmdcSecurity result = null;
		if (securities.size() == 1) {
			result = securities.get(0);
		} else if (securities.size() > 1) {
			for (int i = 0; i < securities.size(); i++) {
				SmdcSecurity sec = securities.get(i);
				if (!SecurityType.index.name().equals(sec.getSecType())) {
					result = sec;
					break;
				}
			}
		}
		return result;
	}
	
	public SmdcSecurity getSecurity(String secId) {
		 return securityService.get(secId);
	}
	
	public Date getPotflioDate(){
		return holidayService.getBusinessDay(new Date(), true);
	}
	
}
